package com.geeguo.ebuilder.core.common.action;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.geeguo.ebuilder.core.common.model.vo.PageListVO;
import com.geeguo.ebuilder.core.common.model.vo.PaginationVO;
import lombok.Data;

import java.util.List;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ActionResult<T> {

    private Integer code;
    private String message;
    private T data;

    public static ActionResult<Boolean> success() {
        ActionResult<Boolean> result = new ActionResult<>();
        result.setCode(ActionResultCode.Success.getCode());
        result.setMessage(ActionResultCode.Success.getMessage());
        result.setData(Boolean.TRUE);
        return result;
    }

    public static <T> ActionResult<T> success(T data) {
        ActionResult<T> result = new ActionResult<>();
        result.setCode(ActionResultCode.Success.getCode());
        result.setMessage(ActionResultCode.Success.getMessage());
        result.setData(data);
        return result;
    }

    public static <T> ActionResult<T> success(String message, T data) {
        ActionResult<T> result = new ActionResult<>();
        result.setCode(ActionResultCode.Success.getCode());
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static <T> ActionResult<PageListVO<T>> page(PaginationVO pagination, List<T> list) {
        PageListVO<T> data = new PageListVO<>(pagination, list);
        return page(data);
    }

    public static <T> ActionResult<PageListVO<T>> page(PageListVO<T> data) {
        ActionResult<PageListVO<T>> result = new ActionResult<>();
        result.setCode(ActionResultCode.Success.getCode());
        result.setMessage(ActionResultCode.Success.getMessage());
        result.setData(data);
        return result;
    }

    public static ActionResult<Boolean> fail() {
        ActionResult<Boolean> result = new ActionResult<>();
        result.setCode(ActionResultCode.Fail.getCode());
        result.setMessage(ActionResultCode.Fail.getMessage());
        result.setData(Boolean.FALSE);
        return result;
    }

    public static <T> ActionResult<T> fail(String message) {
        ActionResult<T> result = new ActionResult<>();
        result.setCode(ActionResultCode.Fail.getCode());
        result.setMessage(message);
        return result;
    }

    public static <T> ActionResult<T> fail(ActionResultCode actionResultCode) {
        ActionResult<T> result = new ActionResult<>();
        result.setCode(actionResultCode.getCode());
        result.setMessage(actionResultCode.getMessage());
        return result;
    }

    public static <T> ActionResult<T> fail(ActionResultCode actionResultCode, String message, T data) {
        ActionResult<T> result = new ActionResult<>();
        result.setCode(actionResultCode.getCode());
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public boolean isSuccess() {
        return ActionResultCode.Success.getCode().equals(code);
    }
}